{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3964653b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 as cv\n",
    "import numpy as np\n",
    "\n",
    "img = cv.imread('../data/j.png',0)\n",
    "kernel = np.ones((5,5),np.uint8)\n",
    "\n",
    "# equal to dilation-erosion\n",
    "gradient = cv.morphologyEx(img, cv.MORPH_GRADIENT, kernel)\n",
    "\n",
    "# dilation-erosion\n",
    "erosion = cv.erode(img,kernel,iterations = 1)\n",
    "dilation = cv.dilate(img,kernel,iterations = 1)\n",
    "dme = dilation - erosion\n",
    "\n",
    "# merge all imgs on horizontal direction\n",
    "res = np.hstack((img, gradient,dme))\n",
    "\n",
    "cv.imshow('res', res)\n",
    "\n",
    "\n",
    "cv.waitKey(0)\n",
    "cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f9cd2333",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 as cv\n",
    "import numpy as np\n",
    "\n",
    "# img = cv.imread('../data/j.png',0)\n",
    "img = cv.imread('../data/jWithWhiteNoiseLeft.png',0)\n",
    "kernel = np.ones((5,5),np.uint8)\n",
    "\n",
    "# equal to input-open\n",
    "tophat = cv.morphologyEx(img, cv.MORPH_TOPHAT, kernel)\n",
    "\n",
    "# input-open\n",
    "opening = cv.morphologyEx(img, cv.MORPH_OPEN, kernel)\n",
    "imo = img - opening\n",
    "\n",
    "# merge all imgs on horizontal direction\n",
    "res = np.hstack((img,opening, tophat,imo))\n",
    "\n",
    "cv.imshow('res', res)\n",
    "\n",
    "\n",
    "cv.waitKey(0)\n",
    "cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "b649f0c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 as cv\n",
    "import numpy as np\n",
    "\n",
    "# img = cv.imread('../data/j.png',0)\n",
    "img = cv.imread('../data/jWithBlackNoiseLeft.png',0)\n",
    "kernel = np.ones((5,5),np.uint8)\n",
    "\n",
    "# equal to close-input\n",
    "blackhat = cv.morphologyEx(img, cv.MORPH_BLACKHAT, kernel)\n",
    "\n",
    "# close-input\n",
    "closing = cv.morphologyEx(img, cv.MORPH_CLOSE, kernel)\n",
    "cmi= closing -img\n",
    "\n",
    "# merge all imgs on horizontal direction\n",
    "res = np.hstack((img,closing, blackhat,cmi))\n",
    "\n",
    "cv.imshow('res', res)\n",
    "\n",
    "\n",
    "cv.waitKey(0)\n",
    "cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe703655",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
